package com.my.code.testcode.POI;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.swing.text.StyleConstants;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

public class CreateTest1 {


    public static void main(String[] args) throws Exception {

        //可以表示xls和xlsx格式文件的类
        XSSFWorkbook workbook = new XSSFWorkbook();
        try {
            //新创建的xls需要新创建新的工作簿，offine默认创建的时候会默认生成三个sheet
            Sheet sheet0 = workbook.createSheet("first sheet");
            Sheet sheet1 = workbook.createSheet("second sheet");

//            for (int j = 0; j < 10; j++) {
//                //创建一行
//                Row row = sheet0.createRow(j);
//                //创建一列
//                for (int i = 0;i<100;i++)
//                    row.createCell(i).setCellValue(i);
//            }

            //时间  row行 cell列
            CreationHelper creationHelper = workbook.getCreationHelper();
            CellStyle cellStyle = workbook.createCellStyle();
            cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-mm-dd hh:mm:ss"));
            Row row = sheet0.createRow(0);
            Cell cell = row.createCell(0);
            cell.setCellValue(new Date());
            cell.setCellStyle(cellStyle);
            cell = row.createCell(1);
            cell.setCellValue(Calendar.getInstance());
            cell.setCellStyle(cellStyle);
            FileOutputStream out = new FileOutputStream("createWorkBook.xlsx");
            workbook.write(out);
            out.close();
            System.out.println("createWorkBook success");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
